/*
 * StockModel.cpp
 *
 *  Created on: 11.02.2009
 *      Author: Konan Andrej
 */
#include <QtCore>
#include "StockModel.h"

StockModel::StockModel( DataBase* db, QObject *parent ) : QAbstractTableModel( parent )
{
        //Print( "StockModel constructor" );
	DB = db;

        toSortFlag = false;
        changeItemsVector();

        connect( DB, SIGNAL(ItemAdded()), this, SLOT(resetModel()) );
        connect( DB, SIGNAL(ItemRemoved()), this, SLOT(resetModel()) );
        connect( DB, SIGNAL(ItemEdited()), this, SLOT(resetModel()) );
        //connect( DB, SIGNAL(ItemAdded()), this, SLOT(changeItemsVector()));
        //connect( DB, SIGNAL(ItemRemoved()), this, SLOT(changeItemsVector()));
}

StockModel::~StockModel()
{
    //~QAbstractTableModel();
}

void StockModel::SetDatabase( DataBase *db )
{
    //disconnect( DB, SIGNAL(ItemAdded()), this, SLOT(resetModel()) );
    //disconnect( DB, SIGNAL(ItemRemoved()), this, SLOT(resetModel()) );

    DB = db;

    changeItemsVector();
    connect( DB, SIGNAL(ItemAdded()), this, SLOT(resetModel()) );
    connect( DB, SIGNAL(ItemRemoved()), this, SLOT(resetModel()) );
    connect( DB, SIGNAL(ItemEdited()), this, SLOT(resetModel()) );
    //connect( DB, SIGNAL(ItemAdded()), this, SLOT(changeItemsVector()));
    //connect( DB, SIGNAL(ItemRemoved()), this, SLOT(changeItemsVector()));

    reset();
}

int StockModel::GetItem(int index, ItemType &item)
{
    if (index >= 0 && index < items.size()) {
        item = items[index];
        return 0;
    } else return range_error;
}

bool caseItemType(const ItemType &i1, const ItemType &i2)
{
    return i1.Get(0) < i2.Get(0);
}

bool caseItemTypeMore(const ItemType &i1, const ItemType &i2)
{
    return i1.Get(0) > i2.Get(0);
}

void StockModel::sort(int column, Qt::SortOrder order)
{
    //sortingColumn = column;
    if ( order == Qt::AscendingOrder )
        qSort( items.begin(), items.end(), caseItemType ); else
            qSort( items.begin(), items.end(), caseItemTypeMore );
    reset();
}

void StockModel::changeItemsVector()
{
    ItemType item;
    items.clear();
    for (int i=0; i<DB->GetItemsCount(); i++){
        DB->GetItem( i, item );
        items.push_back( item );
    }
    if (toSortFlag) qSort( items.begin(), items.end(), caseItemTypeMore );
}

void StockModel::resetModel()
{
    //Output( message, "resetModel() is executed" );
    /*ItemType item;
    items.clear();
    for (int i=0; i<DB->GetItemsCount(); i++){
        DB->GetItem( i, item );
        items.push_back( item );
    }*/
    changeItemsVector();
    reset();
}

int StockModel::rowCount( const QModelIndex &parent ) const
{
	//if (DB->GetItemsCount() == 2) Print( "StockModel::rowCount() is executed" );
	//Print( QString( "items count: " ) + QString::number( DB->GetVisibleNumber() ) );
	return DB->GetItemsCount();
}

int StockModel::columnCount( const QModelIndex &parent ) const
{
	return DB->GetVisibleNumber();
}

QVariant StockModel::data( const QModelIndex &index, int role ) const
{
	//if (DB->GetItemsCount() == 2) Print("StockModel::data() is executed");
	//Print( QString( "Data: " ) + QString::number( index.row() ) );

	if ( !index.isValid() ) return QVariant();

        //ItemType item;

	if ( role == Qt::TextAlignmentRole ){
		return int( /*Qt::AlignRight | */Qt::AlignCenter );
	} else if ( role == Qt::DisplayRole ) {
                //DB->GetItem( index.row(), item );
                //return item.Get( index.column() );
                if ( index.row() >= 0 && index.row() < DB->GetItemsCount() && items.size() > 0 )
                    return items[index.row()].Get( index.column() ); else return QString("");
	}
	return QVariant();
}

QVariant StockModel::headerData( int section, Qt::Orientation orient, int role) const
{
	//Print("StockModel::headerData() is executed");
	if (role != Qt::DisplayRole) return QVariant();
	if ( orient == Qt::Vertical ) return QString::number( section + 1 ); else
		return DB->GetColumnHeader( section );
}

bool StockModel::insertRows ( int row, int count, const QModelIndex & parent )
{
	beginInsertRows(parent, row, row + count);

	endInsertRows();
	return true;
}

bool StockModel::removeRows ( int row, int count, const QModelIndex & parent )
{
	beginRemoveRows(parent, row, row + count);

	endRemoveRows();
	return true;
}
